package com.fl.nj.controller;

import com.fl.nj.entity.SystemConfig;
import com.fl.nj.exception.ApiResponse;
import com.fl.nj.exception.BusinessException;
import com.fl.nj.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统配置Controller
 */
@RestController
@CrossOrigin
@RequestMapping("/api/system-config")
public class SystemConfigController {

    @Autowired
    private SystemConfigService systemConfigService;

    /**
     * 获取所有系统配置
     * 
     * @return 系统配置列表
     */
    @GetMapping
    public ApiResponse<List<SystemConfig>> findAll() {
        List<SystemConfig> configList = systemConfigService.findAll();
        return ApiResponse.success(configList);
    }

    /**
     * 根据ID获取系统配置
     * 
     * @param id 配置ID
     * @return 系统配置对象
     */
    @GetMapping("/{id}")
    public ApiResponse<SystemConfig> findById(@PathVariable Long id) {
        SystemConfig config = systemConfigService.findById(id);
        if (config == null) {
            throw new BusinessException(404, "系统配置不存在");
        }
        return ApiResponse.success(config);
    }

    /**
     * 添加系统配置
     * 
     * @param systemConfig 系统配置对象
     * @return 操作结果
     */
    @PostMapping
    public ApiResponse<SystemConfig> insert(@RequestBody SystemConfig systemConfig) {
        // 参数验证
        if (systemConfig.getConfigKey() == null || systemConfig.getConfigKey().trim().isEmpty()) {
            throw new BusinessException(400, "配置键不能为空");
        }
        if (systemConfig.getConfigValue() == null) {
            throw new BusinessException(400, "配置值不能为空");
        }
        
        int count = systemConfigService.insert(systemConfig);
        if (count <= 0) {
            throw new BusinessException(500, "添加失败");
        }
        
        // 获取插入后的配置信息
        SystemConfig insertedConfig = systemConfigService.findById(systemConfig.getId());
        return ApiResponse.success(insertedConfig);
    }

    /**
     * 更新系统配置
     * 
     * @param id           配置ID
     * @param systemConfig 系统配置对象
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public ApiResponse<SystemConfig> update(@PathVariable Long id, @RequestBody SystemConfig systemConfig) {
        // 验证ID是否匹配
        if (systemConfig.getId() != null && !systemConfig.getId().equals(id)) {
            throw new BusinessException(400, "请求路径中的ID与请求体中的ID不匹配");
        }
        
        // 检查配置是否存在
        SystemConfig existingConfig = systemConfigService.findById(id);
        if (existingConfig == null) {
            throw new BusinessException(404, "系统配置不存在");
        }
        
        // 参数验证
        if (systemConfig.getConfigKey() == null || systemConfig.getConfigKey().trim().isEmpty()) {
            throw new BusinessException(400, "配置键不能为空");
        }
        if (systemConfig.getConfigValue() == null) {
            throw new BusinessException(400, "配置值不能为空");
        }
        
        systemConfig.setId(id);
        int count = systemConfigService.update(systemConfig);
        if (count <= 0) {
            throw new BusinessException(500, "更新失败");
        }
        
        // 获取更新后的配置信息
        SystemConfig updatedConfig = systemConfigService.findById(id);
        return ApiResponse.success(updatedConfig);
    }

    /**
     * 删除系统配置
     * 
     * @param id 配置ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        // 检查配置是否存在
        SystemConfig config = systemConfigService.findById(id);
        if (config == null) {
            throw new BusinessException(404, "系统配置不存在");
        }
        
        int count = systemConfigService.delete(id);
        if (count <= 0) {
            throw new BusinessException(500, "删除失败");
        }
        
        return ApiResponse.success();
    }
}